using System;
using MonoTouch.Dialog;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.ObjCRuntime;


namespace Socialite
{
	// 
	// A MonoTouch.Dialog.Element that renders a PromoterCell
	//
	[Register ("PromoterElement")]
	public class PromoterElement : RootElement , IElementSizing
	{
		static NSString key = new NSString ("promoterelement");
		public Promoter Promoter;
		float _cellHeight;
		
		public PromoterElement (Promoter promoter) : base (null)
		{
			Promoter = promoter;	
		}
	
		// Gets a cell on demand, reusing cells
		public override UITableViewCell GetCell (UITableView tv)
		{
			var cell = tv.DequeueReusableCell (key) as PromoterCell;
			if (cell == null)
			{
				// We have to allocate a cell
				var views = NSBundle.MainBundle.LoadNib ("PromoterCell", tv, null);
				cell = Runtime.GetNSObject (views.ValueAt (0)) as PromoterCell;
			}
			
			cell.BindDataToCell (Promoter);
	
			return cell;
		}
	
		public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path)
		{
			if (MessageBox.Show (String.Empty, "Send request to " + this.Promoter.BasicInfo.FirstName + " for guest list?", MessageBoxButton.YesNo) == MessageBoxResult.Yes) 
			{
				MessageBox.Show ("We will get back to you", "Request sent.", MessageBoxButton.OK); 
			}
		}
	
		public override bool Matches (string text)
		{
			return (Promoter.BasicInfo.FirstName.IndexOf (text, StringComparison.CurrentCultureIgnoreCase) != -1) || 
				(Promoter.BasicInfo.LastName.IndexOf (text, StringComparison.InvariantCultureIgnoreCase) != -1);
		}
	
		public float GetHeight (UITableView tableView, NSIndexPath indexPath)
		{
			if (_cellHeight == 0) {
				var views = NSBundle.MainBundle.LoadNib ("PromoterCell", tableView, null);
				PromoterCell cell = Runtime.GetNSObject (views.ValueAt (0)) as PromoterCell;
					
				_cellHeight = cell.Bounds.Height;
			}
			
			return _cellHeight;
		}
	}
}

